
package com.ttomato.game.mrnom;

import java.util.List;

import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Graphics;
import com.ttomato.game.framework.Screen;
import com.ttomato.game.framework.Input.TouchEvent;

public class MainMenuScreen extends Screen {

    public MainMenuScreen(Game game) {
        super(game);
    }

    @Override
    public void update(float deltaTime) {
        Graphics graphics = mGame.getGraphics();
        List<TouchEvent> touchEvents = mGame.getInput().getTouchEvents();
        mGame.getInput().getKeyEvents();

        int len = touchEvents.size();

        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);

            if (event.type == TouchEvent.TOUCH_UP) {
                if (inBounds(event, 0, graphics.getHeight() - 64, 64, 64)) {
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if (Settings.soundEnabled)
                        Assets.click.play(1);
                }
                if (inBounds(event, 64, 220, 192, 42)) {
                    mGame.setScreen(new GameScreen(mGame));
                    if (Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if (inBounds(event, 64, 220 + 42, 192, 42)) {
                    mGame.setScreen(new HighscoreScreen(mGame));
                    if (Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
                if (inBounds(event, 64, 220 + 84, 192, 42)) {
                    mGame.setScreen(new HelpScreen(mGame));
                    if (Settings.soundEnabled)
                        Assets.click.play(1);
                    return;
                }
            }
        }
    }

    private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if (event.x > x && event.x < x + width - 1 &&
                event.y > y && event.y < y + height - 1)
            return true;
        else
            return false;
    }

    @Override
    public void present(float deltaTime) {
        Graphics graphics = mGame.getGraphics();

        graphics.drawPixmap(Assets.background, 0, 0);
        graphics.drawPixmap(Assets.logo, 32, 32);
        graphics.drawPixmap(Assets.mainMenu, 64, 220);

        if (Settings.soundEnabled)
            graphics.drawPixmap(Assets.buttons, 0, 416, 0, 0, 64, 64);
        else
            graphics.drawPixmap(Assets.buttons, 0, 416, 64, 0, 64, 64);
    }

    @Override
    public void pause() {
        Settings.save(mGame.getFileIO());
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}
